﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.NewPBAuto.IView;
using MINERP.BLL.NewPB;
using MINERP.MODEL.FoundationModel;
using System.Windows.Forms;

namespace MINERP.NewPBAuto.Presenter
{
    class TemplatePresenter
    {
        private IFrmTemplateView _view;
        private NewPBBLL _bll;

        private List<Tb_paperTemplate_Info> _listTemplate;//所有绑定的模板
        private Tb_paperTemplate_Info _curTemplate = null;//当前选中的模板
        private List<PaperFace_Info> _listPaperFace;//纸面
        private List<PaperType_Info> _listPaperType;//纸型
        private List<BoardWidth_Info> _listBoardWidth;//纸宽，不同的纸型有不同的纸宽

        private int _selectedPaperTypeId = 0;


        public TemplatePresenter(IFrmTemplateView view)
        {
            _view = view;
            _bll = new NewPBBLL();

            InitilData();
        }

        #region helpMethods

        private void InitilData()//初始化界面上的数据
        {
            //1、加载模板列表
            BindOther();
            //if (DataTransfer.TemplateList==null||DataTransfer.TemplateList.Count<1)
            //{
            DataTransfer.TemplateList = _bll.GetAllTemplate(DataTransfer.CurPrinter.PrinterID);
            //}
            //_listTemplate = _bll.GetAllTemplate(DataTransfer.CurPrinter.PrinterID);
            _view.BindTemplate(DataTransfer.TemplateList);
        }


        #endregion

        public void SelectedOneTemplate()//选择某一模板后显示详情
        {

            int selectTemplateId = _view.SelectedTemplateId;
            if (selectTemplateId > 0)
            {
                _curTemplate = DataTransfer.TemplateList.FirstOrDefault(t => t.TempLateId == selectTemplateId);
            }

            if (_curTemplate != null)
            {
                _view.ShowTemplateDetail(_curTemplate);
            }
        }

        internal void SelectTemplateOK()//选择模板后返回主界面开始拼板
        {
            if (_curTemplate != null)
            {
                //DataTransfer.curTemplate = _curTemplate;
                DataTransfer.PreSelectTemplateName = _curTemplate.TemplateName;
                _view.Close(true);
            }
            else
            {
                DialogResult Qresut = MessageBox.Show("还未选择模板，是否要关闭？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (Qresut == DialogResult.OK)
                {
                    _view.Close(false);
                }
                else
                {
                    //什么也不做
                }
            }
        }

        internal void DelTemplate()//删除选中的模板
        {
            int curTemplateId = _view.SelectedTemplateId;
            if (curTemplateId > 0)
            {
                DialogResult dr = MessageBox.Show("确认删除该模板", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    bool result = DataTransfer.TemplateList.Remove(DataTransfer.TemplateList.Find(p => p.TempLateId == curTemplateId));
                    if (result)
                    {
                        int rel = _bll.DelTemplate(curTemplateId);
                    }
                    else
                    {
                        MessageBox.Show("删除操作未完成！");
                    }
                }
            }
        }

        internal void AddNewTemplate()//新增模板记录
        {
            string strTemplateName;                     //新模板名
            int paperTypeId, paperFaceId, printerId;    //纸型ID、纸面ID 
            float width, height;                        //板宽、板长
            bool hasBarCode;                            //是否有条码     

            try
            {
                printerId = DataTransfer.CurPrinter.PrinterID;
                strTemplateName = _view.NewTemplateName;
                paperFaceId = _view.NewTPaperFaceId;
                paperTypeId = _view.NewTPaperTypeId;
                width = _view.NewTWidth;
                height = _view.NewHeight;
                hasBarCode = _view.NewHasBarCode;
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的信息");
                return;
            }


            if (!string.IsNullOrEmpty(strTemplateName) && paperTypeId > 0 && paperTypeId > 0 && width > 0 && height > 0)
            {
                _bll.AddNewTemplate(strTemplateName, paperFaceId, paperTypeId, width, height, hasBarCode, printerId);
            }
            InitilData();
        }

        internal void ModifyTemplate()//修改模板
        {
            string strTemplateName;                     //新模板名
            int paperTypeId, paperFaceId, printerId;    //纸型ID、纸面ID 
            float width, height;                        //板宽、板长
            bool hasBarCode;                            //是否有条码
            int curTemplateId = _view.SelectedTemplateId;

            printerId = DataTransfer.CurPrinter.PrinterID;
            strTemplateName = _view.NewTemplateName;
            paperFaceId = _view.NewTPaperFaceId;
            paperTypeId = _view.NewTPaperTypeId;
            width = _view.NewTWidth;
            height = _view.NewHeight;
            hasBarCode = _view.NewHasBarCode;

            if (curTemplateId > 0 && !string.IsNullOrEmpty(strTemplateName) && paperTypeId > 0 && paperTypeId > 0 && width > 0 && height > 0)
            {
                Tb_paperTemplate_Info tempInfo = DataTransfer.TemplateList.FirstOrDefault(t => t.TempLateId == curTemplateId);
                tempInfo.PaperFaceId = paperFaceId;
                tempInfo.PaperTypeId = paperTypeId;
                tempInfo.BoardHeight = height;
                tempInfo.BoardLength = width;
                tempInfo.HasBarCode = hasBarCode;
                if (tempInfo != null)
                {
                    int result = _bll.ModifyTemplate(tempInfo);
                    if (result == 0)
                    {
                        InitilData();
                    }
                }
                else
                {
                    MessageBox.Show("请关闭系统重新打开，再试！");
                }
            }
            else
            {
                MessageBox.Show("输入的修改数据库有误，请选检查,再试！");
            }
        }

        internal void BindOther()//绑定纸型、纸面
        {
            if (_listPaperFace == null)
            {
                _listPaperFace = _bll.GetAllPaperFace();
            }

            if (_listPaperType == null)
            {
                _listPaperType = _bll.GetAllPaperType();
            }

            _view.BindPaperFace(_listPaperFace);
            _view.BindPaperType(_listPaperType);
        }

        internal void BindPaperWidth()//根据相应的纸型绑定纸宽
        {
            int paperTypeid = _view.SelectPaperTypeId;

            if (paperTypeid > 0)
            {
                if (paperTypeid != _selectedPaperTypeId)
                {
                    _selectedPaperTypeId = paperTypeid;
                    _listBoardWidth = _bll.GetSuitPaperWidthByPaperTypeId(_selectedPaperTypeId);
                    _view.BindBoardWidth(_listBoardWidth);
                }
            }
        }
    }
}
